{% set pypi_name = 'monasca-api' %}
{% set source = fetch_source('https://tarballs.openstack.org/monasca-api/monasca-api-master.tar.gz') %}
{% set upstream_version = upstream_version() %}
{% set rpm_release = '1' %}

Name:           {{ py2name() }}
Version:        {{ py2rpmversion() }}
Release:        {{ py2rpmrelease() }}
Summary:        Monasca REST API
License:        {{ license('Apache-2.0') }}
Group:          Development/Languages/Python
URL:            https://wiki.openstack.org/wiki/Monasca
Source0:        {{ source|basename }}
Source2:        api-logging.conf
Source3:        api-config.ini
Source4:        api-wsgi.conf
Source5:        api-wsgi.py
BuildRequires:  fdupes
BuildRequires:  openstack-macros
BuildRequires:  {{ py3('PasteDeploy') }}
BuildRequires:  {{ py3('SQLAlchemy') }}
BuildRequires:  {{ py3('cassandra-driver') }}
BuildRequires:  {{ py3('eventlet') }}
BuildRequires:  {{ py3('falcon') }}
BuildRequires:  {{ py3('fixtures') }}
BuildRequires:  {{ py3('httplib2') }}
BuildRequires:  {{ py3('influxdb') }}
BuildRequires:  {{ py3('python-keystoneclient') }}
BuildRequires:  {{ py3('keystonemiddleware') }}
BuildRequires:  {{ py3('mock') }}
BuildRequires:  {{ py3('monasca-common') }}
BuildRequires:  {{ py3('nose') }}
BuildRequires:  {{ py3('oslo.config') }}
BuildRequires:  {{ py3('oslo.context') }}
BuildRequires:  {{ py3('oslo.db') }}
BuildRequires:  {{ py3('oslo.log') }}
BuildRequires:  {{ py3('oslo.middleware') }}
BuildRequires:  {{ py3('oslo.serialization') }}
BuildRequires:  {{ py3('oslo.upgradecheck') }}
BuildRequires:  {{ py3('oslo.utils') }}
BuildRequires:  {{ py3('oslotest') }}
BuildRequires:  {{ py3('pbr') }}
BuildRequires:  {{ py3('pyparsing') }}
BuildRequires:  {{ py3('python-subunit') }}
BuildRequires:  {{ py3('requests-mock') }}
BuildRequires:  {{ py3('simplejson') }}
BuildRequires:  {{ py3('six') }}
BuildRequires:  {{ py3('stestr') }}
BuildRequires:  {{ py3('testrepository') }}
BuildRequires:  {{ py3('testscenarios') }}
BuildRequires:  {{ py3('testtools') }}
BuildRequires:  {{ py3('voluptuous') }}
Requires:       python3-{{ pypi_name }} = %{version}-%{release}
BuildArch:      noarch
%if 0%{?suse_version}
BuildRequires:  apache2
Requires:       apache2-mod_wsgi-python3
%else
BuildRequires:  python3-mod_wsgi
Requires:       python3-mod_wsgi
%endif

%description
The Monasca API provides a RESTful JSON interface for interacting with
and managing monitoring related resources.

%package -n python3-{{ pypi_name }}
Summary:        Monasca REST API
Group:          Development/Languages/Python
Requires:       {{ py3('PasteDeploy') }}
Requires:       {{ py3('SQLAlchemy') }}
Requires:       {{ py3('eventlet') }}
Requires:       {{ py3('falcon') }}
Requires:       {{ py3('influxdb') }}
Requires:       {{ py3('python-keystoneclient') }}
Requires:       {{ py3('keystonemiddleware') }}
Requires:       {{ py3('monasca-common') }}
Requires:       {{ py3('oslo.config') }}
Requires:       {{ py3('oslo.context') }}
Requires:       {{ py3('oslo.db') }}
Requires:       {{ py3('oslo.log') }}
Requires:       {{ py3('oslo.middleware') }}
Requires:       {{ py3('oslo.serialization') }}
Requires:       {{ py3('oslo.upgradecheck') }}
Requires:       {{ py3('oslo.utils') }}
Requires:       {{ py3('pbr') }}
Requires:       {{ py3('pyparsing') }}
Requires:       {{ py3('simplejson') }}
Requires:       {{ py3('six') }}
Requires:       {{ py3('voluptuous') }}
BuildArch:      noarch

%description -n python3-{{ pypi_name }}
The Monasca API provides a RESTful JSON interface for interacting with
and managing monitoring related resources.

%prep
%autosetup -p1 -n {{ pypi_name }}-{{ upstream_version }}
%py_req_cleanup

%build
%py3_build
PYTHONPATH=. oslo-config-generator \
  --config-file config-generator/monasca-api.conf \
  --output-file etc/monasca-api.conf.sample

%install
%py3_install

# create all necessary directories
install -d -m 755 %{buildroot}%{_usr}/share/{{ pypi_name }}/vertica
install -d -m 755 %{buildroot}%{_usr}/share/{{ pypi_name }}/schema
install -d -m 755 %{buildroot}%{_sysconfdir}/monasca
install -d -m 755 %{buildroot}%{_var}/lib/{{ pypi_name }}
install -d -m 755 %{buildroot}%{_var}/log/{{ pypi_name }}
install -d -m 755 %{buildroot}%{apache_site_dir}

# copy embedded configuration files

## copy configs
install -D -m 644 etc/monasca-api.conf.sample %{buildroot}/%{_sysconfdir}/monasca/monasca-api.conf
install -D -m 640 %{SOURCE2} %{buildroot}%{_sysconfdir}/monasca/api-logging.conf
install -D -m 640 %{SOURCE3} %{buildroot}%{_sysconfdir}/monasca/api-config.ini
# some files from the devstack dir needed to do basic deployment
install -D -m 640 devstack/files/cassandra/monasca_schema.cql %{buildroot}%{_usr}/share/{{ pypi_name }}/schema/
install -D -m 640 devstack/files/vertica/mon_alarms.sql %{buildroot}%{_usr}/share/{{ pypi_name }}/vertica/
install -D -m 640 devstack/files/vertica/mon_metrics.sql %{buildroot}%{_usr}/share/{{ pypi_name }}/vertica/
install -D -m 640 devstack/files/vertica/users.sql %{buildroot}%{_usr}/share/{{ pypi_name }}/vertica/
install -D -m 640 devstack/files/vertica/roles.sql %{buildroot}%{_usr}/share/{{ pypi_name }}/vertica/

## install apache configuration
install -p -D -m 640 %{SOURCE4} %{buildroot}%{apache_site_dir}/{{ pypi_name }}.conf.sample

## install WSGIScriptAlias
install -D -m 755 %{SOURCE5} %{buildroot}%{_bindir}/monasca-api-wsgi.py

# remove duplicates
%fdupes %{buildroot}%{python3_sitelib}/monasca_api

%check
find . -type f -name *.pyc -delete
%{__python3} -m stestr.cli --test-path=./monasca_api/tests run

%pre
%openstack_pre_user_group_create monasca-api monasca /sbin/nologin

%postun
if [ $1 -eq 0 ] ; then
  getent passwd monasca-api > /dev/null
  if [ "$?" == "0" ] ; then
      userdel monasca-api
  fi
fi

%files
%{_bindir}/monasca-api
%{_bindir}/monasca-api-wsgi
%{_bindir}/monasca-api-wsgi.py
%{_bindir}/monasca-status
%{_bindir}/monasca_db

%dir %attr(-, root, monasca) %{_sysconfdir}/monasca
%dir %attr(0750, monasca-api, monasca) %{_var}/lib/{{ pypi_name }}
%dir %attr(0750, monasca-api, monasca) %{_var}/log/{{ pypi_name }}
%dir %attr(0755, monasca-api, monasca) %{_usr}/share/{{ pypi_name }}
%dir %attr(0755, monasca-api, monasca) %{_usr}/share/{{ pypi_name }}/schema
%dir %attr(0755, monasca-api, monasca) %{_usr}/share/{{ pypi_name }}/vertica
%{_usr}/share/{{ pypi_name }}/schema/*
%{_usr}/share/{{ pypi_name }}/vertica/*
%config(noreplace) %attr(0640, root, monasca) %{_sysconfdir}/monasca/monasca-api.conf
%config(noreplace) %attr(0640, root, monasca) %{_sysconfdir}/monasca/api-logging.conf
%config(noreplace) %attr(0640, root, monasca) %{_sysconfdir}/monasca/api-config.ini
%config(noreplace) %attr(0640, root, monasca) %{apache_site_dir}/{{ pypi_name }}.conf.sample

%files -n python3-{{ pypi_name }}
%license LICENSE
%doc README.rst ChangeLog
%{python3_sitelib}/monasca_api
%{python3_sitelib}/*.egg-info

%changelog
